<script setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import api from '@/api'
import { watch } from 'vue'

const $route = useRoute()
const $router = useRouter()

const listData = ref([])
const listTotal = ref(0)

const StatusList = reactive([
  {
    label: '待面试',
    value: 1,
  },
  {
    label: '面试完成',
    value: 2,
  },
  {
    label: '截止',
    value: 3,
  },
])

const departments = ref([])
const posts = ref([])

const filter = reactive({
  departments_id: '',
  posts_id: '',
  status: '',
  keyword: '',
  day: '',
  page: 1,
  pageSize: 10,
})

const filterCurrentDay = () => {
  if (filter.day) {
    filter.day = ''
  } else {
    const now = new Date()
    filter.day = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
  }

  getListData(1)
}

const getListData = async (page) => {
  try {
    const res = await api.get('/recruit/myInterview', {
      params: {
        departments_id: filter.departments_id,
        posts_id: filter.posts_id,
        status: filter.status,
        keyword: filter.keyword,
        day: filter.day,
        page,
        pageSize: filter.pageSize,
      },
    })
    console.log('res', res)
    listData.value = res.data.items
    listTotal.value = res.data.pageInfo.total
    filter.page = page
  } catch (error) {
    console.error(error)
  }
}
getListData(1)

// 部门列表
const getDepartments = async () => {
  console.log('获取部门列表')
  const res = await api.get('/recruit/departmentsList')
  departments.value = res.data
  // if (res.data.length > 0) {
  //   getPosts(res.data[0].id)
  // }
}

// 岗位列表
const getPosts = async (departId) => {
  console.log('获取岗位列表' + departId)
  const res = await api.get(`/recruit/postsList?department_id=${departId}`)
  posts.value = res.data
}

const toJump = (url) => {
  console.log('router', $router.push(url))
  $router.push(url)
}

getDepartments()

watch(
  () => filter.departments_id,
  (newVal) => {
    console.log('change departments_id', newVal)
    getPosts(newVal)
  }
)
</script>

<template>
  <div class="page">
    <div class="header">
      <a-space>
        <!-- <a-button type="primary">今日面试</a-button>
        <a-button type="primary">应聘人员模糊查询</a-button>
        <a-button type="primary">所属部门筛选</a-button>
        <a-button type="primary">所属岗位筛选</a-button>
        <a-button type="primary">面试状态筛选</a-button> -->

        <a-button type="primary"
                  :status="filter.day ? 'warning' : 'primary'"
                  @click="filterCurrentDay">{{ filter.day ? '清除日期' : '今日面试' }}</a-button>
        <!-- <a-date-picker type="date" placeholder="请选择日期" v-model="filter.day" format="YYYY-MM-DD"></a-date-picker> -->
        <a-select v-model="filter.departments_id"
                  placeholder="请选择招聘部门"
                  allow-clear>
          <a-option v-for="(item, index) in departments"
                    :key="index"
                    :value="item.id">{{ item.name }}</a-option>
        </a-select>
        <a-select v-model="filter.posts_id"
                  placeholder="请选择招聘岗位名称"
                  allow-clear>
          <a-option v-for="(item, index) in posts"
                    :key="index"
                    :value="item.id">{{ item.name }}</a-option>
        </a-select>
        <a-select v-model="filter.status"
                  placeholder="面试状态">
          <a-option value="">选择面试状态</a-option>
          <a-option v-for="item in StatusList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}</a-option>
        </a-select>
        <a-input-search :style="{ width: '320px' }"
                        v-model="filter.keyword"
                        placeholder="名称模糊查询"
                        button-text="查询"
                        @search="getListData"
                        search-button />
      </a-space>
    </div>

    <a-table :data="listData"
             @page-change="getListData"
             :pagination="{
              showTotal: true,
              size: 'small',
              total: listTotal,
              pageSize: filter.pageSize,
              current: filter.page,
            }">
      <template #columns>
        <a-table-column title="序号"
                        :width="60">
          <template #cell="{ rowIndex }"> {{ rowIndex + 1 }} </template>
        </a-table-column>
        <a-table-column title="所属部门"
                        data-index="department_name"></a-table-column>
        <a-table-column title="岗位名称"
                        data-index="post_name"></a-table-column>
        <a-table-column title="应聘人员"
                        data-index="worker_name"></a-table-column>
        <a-table-column :sortable="{ sortDirections: ['ascend', 'descend'] }"
                        title="预约面试时间"
                        data-index="reservation_date"></a-table-column>
        <a-table-column :sortable="{ sortDirections: ['ascend', 'descend'] }"
                        title="面试状态"
                        data-index="status_text"></a-table-column>
        <a-table-column title="操作"
                        align="right"
                        :width="200">
          <template #cell="{ record }">
            <a-space>
              <a-button v-if="record.status == 1"
                        type="primary"
                        size="mini"
                        @click="
                  toJump(
                    `/recruit/talentPool/record?reservation_id=${record.reservation_id}&type=1`
                  )
                ">开始面试</a-button>
              <a-button v-if="record.status == 2"
                        type="primary"
                        size="mini"
                        @click="
                  toJump(
                    `/recruit/talentPool/record?reservation_id=${record.reservation_id}&type=2`
                  )
                ">补充面试信息</a-button>
              <a-button type="primary"
                        size="mini"
                        @click="
                  toJump(
                    `/recruit/talentPool/recordDetails?id=${record.reservation_id}`
                  )
                ">面试记录</a-button>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
  </div>
</template>

<style scoped>
.page {
  height: 95%;
}

.header {
  height: 52px;
  display: flex;
}
</style>
